package club.itstudio.IContainer.service;

import club.itstudio.IContainer.entity.Container;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
public class ContainerService {
    private JdbcTemplate jdbcTemplate;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private ContainerMapper containerMapper = new ContainerMapper();

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public boolean insert(Container container) {
        String sql = "insert into container(container_name, user_id, experiment_id, created_time, end_time, " +
                "status, ssh_port, ssh_password) values(?, ?, ?, ?, ?, ?, ?, ?)";
        try {
            return jdbcTemplate.update(sql, container.containerName, container.userID, container.experimentID,
                    container.createdTime, container.endTime, container.status, container.sshPort,
                    container.sshPassword) == 1;
        }catch (DataAccessException e) {
            logger.info("error insert new container:", e);
            return false;
        }
    }

    public Container getByID(int id) {
        String sql = "select * from container where id=?";
        Container container = null;
        try {
            container = jdbcTemplate.queryForObject(sql, containerMapper, id);
        }catch (DataAccessException e) {
            logger.info("error get container by id:" + id, e);
        }
        return container;
    }

    public List<Container> getByUserID(int userID) {
        String sql = "select * from container where user_id=?";
        return jdbcTemplate.query(sql, containerMapper, userID);
    }

    public List<Container> getByExperimentID(int experimentID) {
        String sql = "select * from container where experiment_id=?";
        return jdbcTemplate.query(sql, containerMapper, experimentID);
    }

    public boolean updateStatus(Container container) {
        String sql = "update container set status=? where id=?";
        try {
            return jdbcTemplate.update(sql, container.status, container.id) == 1;
        }catch (DataAccessException e) {
            logger.info("error update container " + container.id + " status ", e);
            return false;
        }
    }

    public Container getByUserIDAndExperimentID(int userID, int experimentID) {
        Container container = null;
        try {
            String sql = "select * from container where user_id=? and experiment_id=?";
            container = jdbcTemplate.queryForObject(sql, containerMapper, userID, experimentID);
        }catch (DataAccessException ignored) {
        }
        return container;
    }

    public List<Container> getExpiredContainer() {
        String sql = "select * from container where status=1 and end_time<?";
        return jdbcTemplate.query(sql, containerMapper, System.currentTimeMillis());
    }

    public List<Container> getByPage(int currentPage, int pageSize) {
        String sql = "select * from container limit ?,?";
        return jdbcTemplate.query(sql, containerMapper, (currentPage - 1) * pageSize, pageSize);
    }

    public Integer getContainerCount(){
        String sql = "select count(*) from container";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }
}

class ContainerMapper implements RowMapper<Container> {
    @Override
    public Container mapRow(ResultSet resultSet, int i) throws SQLException {
        Container container = new Container();
        container.id = resultSet.getInt("id");
        container.containerName = resultSet.getString("container_name");
        container.userID = resultSet.getInt("user_id");
        container.experimentID = resultSet.getInt("experiment_id");
        container.createdTime = resultSet.getLong("created_time");
        container.endTime = resultSet.getLong("end_time");
        container.status = resultSet.getInt("status");
        container.sshPort = resultSet.getInt("ssh_port");
        container.sshPassword = resultSet.getString("ssh_password");
        return container;
    }
}
